package com.jhd.fuzhou.view.mail;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.LoginService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.PersonInfoVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MailListDeptActivity extends BaseActivity {
    @BindView(R.id.tv_title)TextView tv_title;
    @BindView(R.id.rv_person)RecyclerView rv_person;
    private String title,deptId,orgId;
    private Context context=this;
    private List<PersonInfoVo> personInfoVoList;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_mail_list_dept);
    }

    @Override
    protected void processLogic() {
        onBack();
        title=getIntent().getStringExtra("name");
        deptId=getIntent().getStringExtra("deptId");
        orgId=getIntent().getStringExtra("orgId");
        tv_title.setText(title);
        getPersonInfoList();
    }
    private void getPersonInfoList(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        LoginService vs = retrofit.create(LoginService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("deptId",StringUtil.isNullOrEmpty(deptId)?"":deptId).addParam("orgId",StringUtil.isNullOrEmpty(orgId)?"":orgId);
        Call<JsonForResult> jsonForResultCall = vs.getPersonInfoList(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            personInfoVoList = JSON.parseArray(jsonForResult.getData().toString(), PersonInfoVo.class);
                            LinearLayoutManager manager=new LinearLayoutManager(context);
                            rv_person.setLayoutManager(manager);
                            PersonAdapter adapter=new PersonAdapter(personInfoVoList);
                            rv_person.setAdapter(adapter);
                        }

                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
        private List<PersonInfoVo> personInfoVoList;

        public PersonAdapter(List<PersonInfoVo> personInfoVoList) {
            this.personInfoVoList = personInfoVoList;
        }

        @NonNull
        @Override
        public PersonAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mail_list_person, parent, false);
            PersonAdapter.ViewHolder viewHolder = new PersonAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull PersonAdapter.ViewHolder holder, int position) {
            holder.tv_dept.setText(StringUtil.isNullOrEmpty(personInfoVoList.get(position).getDeptName())?"":personInfoVoList.get(position).getDeptName());
            holder.tv_name.setText(StringUtil.isNullOrEmpty(personInfoVoList.get(position).getName())?"":personInfoVoList.get(position).getName());
            holder.tv_contactNumber.setText(StringUtil.isNullOrEmpty(personInfoVoList.get(position).getMobileNo())?"":personInfoVoList.get(position).getMobileNo());
        }

        @Override
        public int getItemCount() {
            return personInfoVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_name;TextView tv_contactNumber;TextView tv_dept;
            LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_dept = itemView.findViewById(R.id.tv_dept);
                this.tv_name = itemView.findViewById(R.id.tv_name);
                this.tv_contactNumber = itemView.findViewById(R.id.tv_contactNumber);
            }
        }
    }
}